var czml = [{

  "id": "document",

  "name": "CZML Path",

  "version": "1.0",

  "clock": {

    "currentTime": "2012-08-04T10:00:00Z",

    "multiplier": 10

  }

},
  {

  "id": "path",

  "name": "path with GPS flight data",

  "description": "<p>Hang gliding flight log data from Daniel H. Friedman.<br>Icon created by Larisa Skosyrska from the Noun Project</p>",

  "path": {

    "material": {

      "polylineOutline": {

        "color": {

          "rgba": [255, 0, 255, 255]

        },

        "outlineColor": {

          "rgba": [255, 0, 255, 255]

        },

        "outlineWidth": 5

      }

    },

    "width": 8,

    "leadTime": 0, // 路线提前执行时间

    "trailTime": 100000, // 路线跟踪时间

    "resolution": 5,

    //"arcType" : "RHUMB",

  },


  "position": {

    "epoch": "2012-08-04T10:00:00Z",

    "cartographicDegrees": [
      0.0, 118.87841653400005, 30.956798, 0.0,
      10.0, 118.87826541800007, 30.956799, 0.0,
    ]
  }
}];

viewer.dataSources.add(Cesium.CzmlDataSource.load(czml)).then(function (ds) {
  viewer.trackedEntity = ds.entities.getById('path');//追踪实体
});

// 动态赋值
var i = 30.956800;
var a = 20;
var b = 118.8774481050001;
setInterval(function () {
  i += 0.001 * Math.random();
  b += 0.001 * Math.random();
  a += 10;
  //路径最后添加节点
  czml[1].position.cartographicDegrees.push(a, b, i, 0);// 赋值当前最新行驶路线

  czml[0].clock.currentTime = viewer.clock.currentTime.toString(); // 修改当前时间，防止从头重新开始执行

  //清空之前数据，否则数据越来越多

  viewer.entities.removeById("path");

  //重新添加修改后的数据

  viewer.dataSources.add(Cesium.CzmlDataSource.load(czml));

}, 1000);



function drawPolyline() {
  var floatingPoint;
  var activePolyline;
  var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  handler.setInputAction(function(click) {
    //var position = viewer.scene.pickPosition(click.position);
    if(Cesium.defined(location.cartesian)){
      var cartesian = location.cartesian;
      if(activeShapePoints.length === 0){
        floatingPoint = creatPoint(cartesian);
        activeShapePoints.push(cartesian);
        var dynamicPositions = new Cesium.CallbackProperty(function() {
          return activeShapePoints;
        },false);
        activePolyline = createPolyline(dynamicPositions);
      }
      activeShapePoints.push(cartesian);
      creatPoint(cartesian);
    }
  },Cesium.ScreenSpaceEventType.LEFT_CLICK);
  handler.setInputAction(function(movement) {
    if(Cesium.defined(floatingPoint)){
      if(Cesium.defined(location.endPosition)){
        floatingPoint.position.setValue(location.endPosition);
        activeShapePoints.pop();
        activeShapePoints.push(location.endPosition);
      }
    }
  },Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  handler.setInputAction(function(movement) {
    handler.destroy();
    for(var i=0;i<Points.length;i++){
      viewer.entities.remove(Points[i]);
    }
    Points = [];
  },Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);

  function createPolyline(positionData) {
    var polyline;
    polyline = viewer.entities.add({
      name : 'polyline',
      polyline : {
        positions : positionData,
        //在地形上绘制多段线，但是在3dtilset模型上无效
        clampToGround : false,
        followSurface : false,
        material: Cesium.Color.RED,
        width : 3
      }
    });
    return polyline;
  }
}